﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.DirectX;

namespace ParticleEditor
{

    public class Particle
    {

        // public variables
        #region
        // BOOLEAN
        public bool isActive = false; // is the particle Active?

        // FLOAT
        public float currAge; // current particle age
        public float maxAge; // max particle age

        public float startRotation; // start rotation angle
        public float currRotation; // current rotation angle
        public float endRotation; // ending rotation angle

        public float rotationReducer; // used to modify rotation over time
        public float colorReducerA; // used to modify Alpha colors over time
        public float colorReducerR; // used to modify Red colors over time
        public float colorReducerG; // used to modify Green colors over time
        public float colorReducerB; // used to modify Blue colors over time

        public float currA; // current Alpha color
        public float currR; // current Red color
        public float currG; // current Green color
        public float currB; // current Blue color

        public float diffA; // difference between start and end Alpha colors
        public float diffR; // difference between start and end Red colors
        public float diffG; // difference between start and end Green colors
        public float diffB; // difference between start and end Blue colors

        // VECTOR2
        public Vector2 position; // Position of the particle

        public Vector2 startVelocity; // starting velocity
        public Vector2 currVelocity; // current velocity
        public Vector2 endVelocity; // ending velocity

        public Vector2 startScale; // starting scale
        public Vector2 currScale; // current scale
        public Vector2 endScale; // ending scale

        public Vector2 scaleReducer; // used to modify scale over time
        public Vector2 velocityReducer; // used to modify velocity over time

        // COLOR
        public Color startColor; // starting color
        public Color currColor; // current color
        public Color endColor; // ending color

        // emitter variable to access updated NUD info
        Emitter emmit = new Emitter(); // see overloaded ctor
        #endregion

        // overloaded ctor (attach to current emitter and set variables)
        public Particle(Emitter import)
        {
            // attach emmit to current emitter
            emmit = import;

            // FLOATs
            maxAge = import.maxParticleAge;
            startRotation = import.startRotation;
            currRotation = import.startRotation;
            endRotation = import.endRotation;

            // VECTOR2s
            position = import.position;
            startVelocity = import.startVelocity;
            currVelocity = import.startVelocity;
            endVelocity = import.endVelocity;
            startScale = import.startScale;
            endScale = import.endScale;
            currScale = import.startScale;

            // COLORs
            startColor = import.startColor;
            currColor = import.startColor;
            endColor = import.endColor;
        }

        // update the particle
        public void Update(float elapsedTime)
        {
            if (maxAge == 0)
                maxAge = .01f;
            // color blending          
            // lerp each color variable (ARGB)
            currA = startColor.A + (endColor.A - startColor.A) * (currAge / maxAge);
            currR = startColor.R + (endColor.R - startColor.R) * (currAge / maxAge);
            currG = startColor.G + (endColor.G - startColor.G) * (currAge / maxAge);
            currB = startColor.B + (endColor.B - startColor.B) * (currAge / maxAge);
            // set new color
            currColor = Color.FromArgb((int)currA, (int)currR, (int)currG, (int)currB);

            // scale blending
            currScale = Vector2.Lerp(startScale, endScale, currAge / maxAge);

            // rotation blending
            currRotation += (emmit.startRotation + (emmit.endRotation - emmit.startRotation) * (currAge / maxAge)) *elapsedTime;

            // velocity blending
            currVelocity = Vector2.Lerp(startVelocity, endVelocity, currAge / maxAge);

            // update particle position
            position += (currVelocity * elapsedTime);
            // update particle age
            currAge += elapsedTime;
        }
    }
}
